<template>
  <div class="user">
    <search-page
      :searchFormConfig="searchFormConfig"
      @handleReset="handleResetClick"
      @handleQuery="handleQueryClick"
    />

    <page-content
      ref="pageContentRef"
      :pageContentConfig="pageTableConfig"
      pageName="users"
      @handleCreateData="handleCreateDataClick"
      @handleEditData="handleEditDataClick"
    >
      <template #status="scope">
        <el-button size="small" :type="scope.row.enable ? 'success' : 'danger'">
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
    </page-content>

    <page-model
      ref="pageModelRef"
      :pageModelConfig="pageModelConfigRef"
      :defaultInfo="defaultInfo"
      pageName="users"
    ></page-model>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent } from 'vue'
import { useStore } from '@/store'

import { searchFormConfig } from './config/search.config'
import { pageTableConfig } from './config/pageContent.config'
import { pageModelConfig } from './config/pageModel.config'

import { queryUseSearchPage } from '@/hooks/use-search-page'
import { usePageModel } from '@/hooks/use-page-model'

export default defineComponent({
  components: {},
  name: 'user',
  setup() {
    const [pageContentRef, handleResetClick, handleQueryClick] =
      queryUseSearchPage()

    //密码 组件的显示与不显示控制
    const callBackNewFn = () => {
      const passwordItem = pageModelConfig.formItems.find(
        (item) => item.field === 'password'
      )
      if (passwordItem) {
        passwordItem.isHidden = false
      }
    }

    const callBackEditFn = () => {
      const passwordItem = pageModelConfig.formItems.find(
        (item) => item.field === 'password'
      )
      if (passwordItem) {
        passwordItem.isHidden = true
      }
    }

    //动态处理部门以及角色下拉选项
    const store = useStore()
    const pageModelConfigRef = computed(() => {
      const departmentItem = pageModelConfig.formItems.find(
        (item) => item.field === 'departmentId'
      )
      departmentItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })

      const roleItem = pageModelConfig.formItems.find(
        (item) => item.field === 'roleId'
      )

      roleItem!.options = store.state.entireRole.map((item) => {
        return { title: item.name, value: item.id }
      })

      return pageModelConfig
    })

    const [
      pageModelRef,
      defaultInfo,
      handleCreateDataClick,
      handleEditDataClick
    ] = usePageModel(callBackNewFn, callBackEditFn)

    return {
      searchFormConfig,
      pageTableConfig,
      pageContentRef,
      handleResetClick,
      handleQueryClick,
      pageModelConfigRef,

      pageModelRef,
      defaultInfo,
      handleCreateDataClick,
      handleEditDataClick
    }
  }
})
</script>

<style scoped></style>
